
//
// 120824 - AH - Created.
//

package org.japril.jhut.swing;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import org.japril.jhut.framework.JhutTest;

/**
 * <p>
 *   Adapts a 
 *   {@link JhutTest} to a
 *   {@link JTree} view.
 * </p>
 * @author Arne Halvorsen (AH) 
 */
public final class TestTreeModel implements TreeModel
{
  /**
   * <p>
   *   The empty model shared.
   * </p>
   */
  public static final TestTreeModel EMPTY_MODEL=new TestTreeModel();
  
  private final List<JhutTest> m_forest=new ArrayList<JhutTest>();
  
  /**
   * <p>
   *   Creates the empty model.
   * </p>
   */
  private TestTreeModel(){}
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param roots Roots of hierarchy.
   */
  public TestTreeModel(List<JhutTest> roots)
  {
    if (roots==null)
    {
      throw new NullPointerException("root");
    }
    
    int n=roots.size();
    for (int i=0; i<n; i++)
    {
      JhutTest t=roots.get(i);
      if (t==null)
      {
        throw new NullPointerException("roots["+i+"]");
      }
      
      m_forest.add(t);
    }
  }
  
  /**
   * <p>
   *   Gets roots of forest.
   * </p>
   * @return Roots.
   */
  public List<JhutTest> getRoots(){ return m_forest; }
  
  // Specified in TreeModel.
  public Object getChild(Object parent, int idx)
  {
    if (parent==this)
    {
      return m_forest.get(idx);  
    }
    else
    {
      return ((JhutTest)parent).getChild(idx);
    }
  }

  // Specified in TreeModel.
  public int getChildCount(Object parent)
  {
    if (parent==this)
    {
      return m_forest.size();  
    }
    else
    {
      return ((JhutTest)parent).getChildCount();
    }
  }

  // Specified in TreeModel.
  public int getIndexOfChild(Object parent, Object child)
  {
    if (parent==this)
    {
      return m_forest.indexOf(child);
    }
    else
    {
      return ((JhutTest)parent).getIndexOfChild(child);
    }
  }

  // Specified in TreeModel.
  public Object getRoot(){ return this; }

  // Specified in TreeModel.
  public boolean isLeaf(Object node)
  { 
    if (node==this)
    {
      return false;
    }
    else
    {
      return ((JhutTest)node).isLeaf();
    } 
  }

  // Specified in TreeModel.
  public void addTreeModelListener(TreeModelListener l) 
  {  
  }
 
  // Specified in TreeModel.
  public void removeTreeModelListener(TreeModelListener l)
  {  
  }

  // Specified in TreeModel.
  public void valueForPathChanged(TreePath path, Object newValue) 
  {  
  }

}
